﻿namespace Web.Control.WWW.Answers
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Lx.Web.Common.ControlsUI;
    using System.Collections.Generic;
    using Model.WWW_WWW.Answers;
    using BLF.WWW_WWW.Answer;
    using System.Collections;

    /// <summary>
    /// 我的问题、我回答的问题、朋友的问题、朋友回答的问题
    /// </summary>
    public class MyQA : BaseControl
    {        
        /// <summary>
        /// 当前类型（1:我的问题,2:我回答的问题,3:朋友的问题,4:朋友回答的问题）
        /// </summary>
        protected int CurType = 1;
        /// <summary>
        /// 绑定问题学习语言数据
        /// </summary>
        protected Repeater rptLL;
        /// <summary>
        /// 流行标签
        /// </summary>
        protected Repeater rptTags;
        /// <summary>
        /// 好友问题（3个）
        /// </summary>
        protected Repeater rptFQ;
        /// <summary>
        /// 好友回答问题（3个）
        /// </summary>
        protected Repeater rptFA;
        /// <summary>
        /// 我的问题
        /// </summary>
        protected Repeater rptMyQ;
        /// <summary>
        /// 我回答的问题或好友问题或好友回答的问题
        /// </summary>
        protected Repeater rptNoMy;
        /// <summary>
        /// 数据数量
        /// </summary>
        protected int RecordCount = 0;
        /// <summary>
        /// tag列表
        /// </summary>
        protected List<string> TagList;
        /// <summary>
        /// Cookie保存的学习语言
        /// </summary>
        protected string CokLearn = "";
        /// <summary>
        /// Cookie保存的使用语言
        /// </summary>
        protected string CokSpeak = "";
        protected int pageIndex = 1;
        private string memberno = "";

        protected string C0037 = "";
        protected string C0105 = "";        
        protected string AS063 = "";
        protected string AS139 = "";
        protected string AS140 = "";
        protected string AS142 = "";
        protected override void OnLoad(EventArgs e)
        {
            memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                Page.Response.Redirect(WWWHost + "/User/login.htm?ReturnUrl=" + Page.Request.Url.AbsoluteUri.Split('?')[0]);
                return;
            }
            if (!string.IsNullOrEmpty(Request["page"]))
            {
                if (Request["page"] == "L")
                {
                    pageIndex = 999;
                }
                else
                {
                    int.TryParse(Request["page"], out pageIndex);
                    if (pageIndex == 0)
                    {
                        pageIndex = 1;
                    }
                }                
            }
            if (Request.Cookies["italkiLearn"] != null)
            {
                CokLearn = Request.Cookies["italkiLearn"].Value;
            }
            if (Request.Cookies["italkiSpeak"] != null)
            {
                CokSpeak = Request.Cookies["italkiSpeak"].Value;
            }
            int pageSize=20;
            List<QuestionModel> questionList = new List<QuestionModel>();
            ArrayList outList = new ArrayList();

            if (!string.IsNullOrEmpty(Request["MDType"]))
            {
                string qtype = Request["MDType"].ToString().Trim();
                switch (qtype)
                {
                    case "1":
                        CurType = 1;
                        questionList = Answers.GetMyQuestionList(memberno, pageIndex, pageSize, out outList);
                        break;
                    case "2":
                        CurType = 2;
                        questionList = Answers.GetMyAnswersQList(memberno, pageIndex, pageSize, out outList);
                        break;
                    case "3":
                        CurType = 3;
                        questionList = Answers.GetFriendsAQList(1, memberno, pageIndex, pageSize, out outList);
                        break;
                    case "4":
                        CurType = 4;
                        questionList = Answers.GetFriendsAQList(0, memberno, pageIndex, pageSize, out outList);
                        break;
                    default:
                        questionList = Answers.GetMyQuestionList(memberno, pageIndex, pageSize, out outList);
                        break;
                }
            }
            else
            {
                questionList = Answers.GetMyQuestionList(memberno, pageIndex, pageSize, out outList);
            }
            if (questionList != null)
            {
                C0037 = GetText("C0037");
                C0105 = GetText("C0105");
                AS063 = GetText("AS063");
                AS139 = GetText("AS139");
                AS140 = GetText("AS140");
                AS142 = GetText("AS142");

                RecordCount = (int)outList[0];
                int pCount = (int)Math.Ceiling(((decimal)RecordCount) / ((decimal)pageSize));
                if (pageIndex > pCount) pageIndex = pCount;
                if (CurType == 3 || CurType == 4)
                {
                    if (rptLL != null)
                    {
                        List<BLF.LanguageObj> lanObjList = new List<BLF.LanguageObj>();                       
                        lanObjList.AddRange(BLF.StaticData.GetPopLanguageList);
                        rptLL.DataSource = lanObjList;
                        rptLL.DataBind();
                    }
                    TagList = outList[1] as List<string>;                    
                }
                else
                {
                    if (rptFQ != null)
                    {
                        rptFQ.DataSource = outList[1];
                        rptFQ.DataBind();
                    }
                    if (rptFA != null)
                    {
                        rptFA.DataSource = outList[2];
                        rptFA.DataBind();
                    }
                }
                if (CurType==1 && rptMyQ != null)
                {
                    rptMyQ.DataSource = questionList;
                    rptMyQ.DataBind();
                }
                else if (rptNoMy != null)
                {
                    rptNoMy.DataSource = questionList;
                    rptNoMy.DataBind();
                }
            }            
        }
        /// <summary>
        /// 获取不同状态消息
        /// </summary>
        /// <param name="status">问题状态</param>
        /// <param name="datetime">DiffLastUpdate时间</param>
        /// <param name="mno">会员号</param>
        /// <param name="nickname">昵称名</param>
        /// <returns>string</returns>
        protected string QMessage(string status, string datetime,string mno,string nickname)
        {
            string mesStr = "";            
            switch (status)
            {
                case "0":
                    mesStr = GetText("AS169", new string[] {mno, nickname, FormatDiffDate(GetText("C0037"), datetime) });
                    break;
                case "1":
                    if (memberno != mno)
                    {
                        mesStr = GetText("AS170", new string[] {mno, nickname, FormatDiffDate(GetText("C0035"), datetime) });
                    }
                    else {
                        mesStr = FormatDiffDate(GetText("C0037"), datetime);
                    }
                    break;
                case "2":
                    mesStr = GetText("AS171") + FormatDiffDate(GetText("C0037"), datetime);
                    break;
                case "3":
                    mesStr = FormatDiffDate(GetText("C0037"), datetime);
                    break;
            }
            return mesStr;
        }
    }
}
